class PubSub {
  constructor() {
    this.subscribers = {};
  }

  // 订阅方法
  subscribe(event, callback) {
    if (!this.subscribers[event]) {
      this.subscribers[event] = [];
    }
    this.subscribers[event].push(callback);
  }

  // 发布方法
  publish(event, data) {
    if (this.subscribers[event]) {
      this.subscribers[event].forEach(callback => callback(data));
    }
  }

  // 取消订阅方法
  unsubscribe(event, callback) {
    if (!this.subscribers[event]) return;

    this.subscribers[event] = this.subscribers[event].filter(subscriber => subscriber !== callback);
  }
}


export default new PubSub();